<html>
<head><meta charset="utf-8"><title>Passing dyn by value · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html">Passing dyn by value</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="245741526"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245741526" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245741526">(Jul 12 2021 at 20:13)</a>:</h4>
<p>So we had this "dyn rvalues" RFC that aimed to allow unsized things on the stack. But I have come to the opinion it was too maximal.</p>



<a name="245741538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245741538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245741538">(Jul 12 2021 at 20:13)</a>:</h4>
<p>In particular, it is not compatible with <code>async fn</code></p>



<a name="245741552"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245741552" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245741552">(Jul 12 2021 at 20:13)</a>:</h4>
<p>Since in an async fn we can't do <code>alloca</code></p>



<a name="245741603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245741603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245741603">(Jul 12 2021 at 20:13)</a>:</h4>
<p>I am thinking again about a scaled back version, one that permits you to pass ownership of <code>dyn</code> values <em>to functions</em> but not to manipulate them <em>within</em> a function</p>



<a name="245741678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245741678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245741678">(Jul 12 2021 at 20:14)</a>:</h4>
<p>In particular this would enable code like this</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="nc">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="245741718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245741718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245741718">(Jul 12 2021 at 20:14)</a>:</h4>
<p>Coupled with a <code>size_of</code> operation, one could also do things like</p>



<a name="245741764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245741764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245741764">(Jul 12 2021 at 20:14)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="nc">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">())</span><span class="w"> </span>-&gt; <span class="nb">Box</span><span class="o">&lt;</span><span class="k">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">()</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="245741816"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245741816" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245741816">(Jul 12 2021 at 20:15)</a>:</h4>
<p>That seems feasible.</p>



<a name="245741823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245741823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245741823">(Jul 12 2021 at 20:15)</a>:</h4>
<p>part of why I'm thinking about this again is thinking about portable runtime APIs, and wanting to have the ability to have <code>dyn</code> traits that take <code>dyn</code> values by argument</p>



<a name="245741861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245741861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245741861">(Jul 12 2021 at 20:15)</a>:</h4>
<p>so as to be both ergonomic and more compatible with no-std</p>



<a name="245741944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245741944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245741944">(Jul 12 2021 at 20:15)</a>:</h4>
<p>It's largely orthogonal to being able to do things like create an array on the stack, but in theory you <em>could</em> do something like <code>alloc_array(..., |array| some_code(array))</code> with that, at least outside of async.</p>



<a name="245742029"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245742029" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245742029">(Jul 12 2021 at 20:16)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/213817-t-lang/topic/Passing.20dyn.20by.20value/near/245741861">said</a>:</p>
<blockquote>
<p>so as to be both ergonomic and more compatible with no-std</p>
</blockquote>
<p>The latter seems especially exciting. I'd love to make it possible to have trait objects without boxing.</p>



<a name="245742074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245742074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245742074">(Jul 12 2021 at 20:16)</a>:</h4>
<p>I also just like the story that one can write</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="nc">impl</span><span class="w"> </span><span class="n">Trait</span><span class="p">)</span><span class="w"></span>
</code></pre></div>
<p>and</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="nc">dyn</span><span class="w"> </span><span class="n">Trait</span><span class="p">)</span><span class="w"></span>
</code></pre></div>
<p>with (nearly) equal ease.</p>



<a name="245742088"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245742088" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245742088">(Jul 12 2021 at 20:16)</a>:</h4>
<p>The Linux kernel would also benefit from that; it will have enough of alloc to have <code>Box</code>, but it'll still prefer to avoid allocating if possible.</p>



<a name="245742179"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245742179" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245742179">(Jul 12 2021 at 20:17)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/213817-t-lang/topic/Passing.20dyn.20by.20value/near/245742074">said</a>:</p>
<blockquote>
<p>I also just like the story that one can write</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="nc">impl</span><span class="w"> </span><span class="n">Trait</span><span class="p">)</span><span class="w"></span>
</code></pre></div>
<p>and</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">x</span>: <span class="nc">dyn</span><span class="w"> </span><span class="n">Trait</span><span class="p">)</span><span class="w"></span>
</code></pre></div>
<p>with (nearly) equal ease.</p>
</blockquote>
<p>This also seems like something only possible now that we've mandated the <code>dyn</code> keyword for that. Doing that with just <code>Trait</code> seems error-prone.</p>



<a name="245744113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245744113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245744113">(Jul 12 2021 at 20:31)</a>:</h4>
<p>So basically the rule would be unsized values are allowed as parameters, but not as locals and return values on the MIR level. That works fine even without alloca. cg_clif already has this implemented. (Cranelift doesn't support alloca) This is already pretty much implemented as <code>#![feature(unsized_fn_params)]</code> I believe.</p>



<a name="245745125"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245745125" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245745125">(Jul 12 2021 at 20:39)</a>:</h4>
<p>Yes, that would be the rule, and the fact that it is indeed fairly easy to implement is the main reason I like it</p>



<a name="245745234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245745234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245745234">(Jul 12 2021 at 20:40)</a>:</h4>
<p>There are some other cases you could conceivably manage -- e.g., using a <code>let</code> to "move" out from some outer location</p>



<a name="245746545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245746545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245746545">(Jul 12 2021 at 20:50)</a>:</h4>
<p>When such a "move" rule would be implemented it would preferably happen in the form of a MIR transformation that preserves the only parameters are unsized rule. Otherwise every backend would have to contain this complex logic or fallback to alloca if it supports it.</p>



<a name="245747132"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245747132" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245747132">(Jul 12 2021 at 20:54)</a>:</h4>
<p>Is it really possible to use such a value though? Not being able to memcpy a rust value seems like it would be irretrievably hobbled. How do you move it elsewhere?</p>



<a name="245747246"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245747246" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245747246">(Jul 12 2021 at 20:55)</a>:</h4>
<p>or is this some kind of Pin-alike?</p>



<a name="245749746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245749746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245749746">(Jul 12 2021 at 21:07)</a>:</h4>
<p>I assume that the idea would be to keep it in the same place, but update the places the local is referenced from to point to the original parameter or a field of the parameter.</p>



<a name="245750454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245750454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245750454">(Jul 12 2021 at 21:10)</a>:</h4>
<p>I don't see how that would work if you want to forward the argument to another function, for example</p>



<a name="245750541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245750541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245750541">(Jul 12 2021 at 21:11)</a>:</h4>
<p>Unsized values are always passed by-reference</p>



<a name="245750612"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245750612" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245750612">(Jul 12 2021 at 21:11)</a>:</h4>
<p>I see, so it's really <code>Box&lt;dyn Trait&gt;</code> at the abi level?</p>



<a name="245750687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245750687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245750687">(Jul 12 2021 at 21:12)</a>:</h4>
<p>Pretty much. Except that the caller is responsible for "freeing" the memory and not the callee.</p>



<a name="245750688"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245750688" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245750688">(Jul 12 2021 at 21:12)</a>:</h4>
<p>(actually it's unclear whether it's <code>Box&lt;dyn Trait&gt;</code> or <code>&amp;mut dyn Trait</code>)</p>



<a name="245750763"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245750763" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245750763">(Jul 12 2021 at 21:13)</a>:</h4>
<p>but then what advantage does this actually have over <code>&amp;mut dyn Trait</code> arguments?</p>



<a name="245750772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245750772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245750772">(Jul 12 2021 at 21:13)</a>:</h4>
<p><code>&amp;mut dyn Trait</code> wouldn't allow moving out of the argument, so it is more like <code>Box&lt;dyn Trait&gt;</code>.</p>



<a name="245750781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245750781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245750781">(Jul 12 2021 at 21:13)</a>:</h4>
<p>dropping?</p>



<a name="245750901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245750901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245750901">(Jul 12 2021 at 21:14)</a>:</h4>
<p>For example an <code>&amp;mut dyn FnOnce()</code> argument is uncallable. An <code>dyn FnOnce()</code> or <code>Box&lt;dyn FnOnce()&gt;</code> argument is callable.</p>



<a name="245751748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245751748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245751748">(Jul 12 2021 at 21:21)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/213817-t-lang/topic/Passing.20dyn.20by.20value/near/245750901">said</a>:</p>
<blockquote>
<p>For example an <code>&amp;mut dyn FnOnce()</code> argument is uncallable. An <code>dyn FnOnce()</code> or <code>Box&lt;dyn FnOnce()&gt;</code> argument is callable.</p>
</blockquote>
<p>So the caller (creator of the <code>dyn Trait</code> value) is responsible for freeing the memory, but the callee is responsible for dropping (so neither <code>Box&lt;T&gt;</code> nor <code>&amp;mut T</code> completely describes this type). In that case, I don't see how you could forward to a function accepting a <code>Box&lt;dyn Trait&gt;</code>, since that function will want to free the memory behind the box and can't since it's actually stack memory</p>



<a name="245751991"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245751991" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245751991">(Jul 12 2021 at 21:23)</a>:</h4>
<p>In that case you need to <code>Box</code> it first using <code>Box::new</code>.</p>



<a name="245752116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245752116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245752116">(Jul 12 2021 at 21:24)</a>:</h4>
<p>So part of this is <code>Box::new</code> becoming magic so that it can move out of these unsized parameters?</p>



<a name="245752184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245752184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245752184">(Jul 12 2021 at 21:25)</a>:</h4>
<p>Using <code>size_of_val</code> to determine the allocation size and <code>copy_nonoverlapping</code> to copy an arbitrarily sized region of memory should work I think. Then <code>mem::forget</code> can be used to prevent a drop of the original parameter.</p>



<a name="245752295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245752295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245752295">(Jul 12 2021 at 21:26)</a>:</h4>
<p>The signature of <code>Box::new</code> currently requires that its argument is <code>Sized</code>; are unsized parameters also being considered here or just parameters of the <code>dyn Trait</code> variety?</p>



<a name="245752423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245752423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245752423">(Jul 12 2021 at 21:26)</a>:</h4>
<p>(sorry for all the clarification questions)</p>



<a name="245752462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245752462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245752462">(Jul 12 2021 at 21:27)</a>:</h4>
<p>It is currently not accepted, but I was saying how I think it could be impelemented in the future.</p>



<a name="245752523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245752523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245752523">(Jul 12 2021 at 21:27)</a>:</h4>
<p>That is <code>Box::new</code> taking <code>T: ?Sized</code>.</p>



<a name="245752640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245752640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245752640">(Jul 12 2021 at 21:28)</a>:</h4>
<p>Ah, I guess that all the considerations about <code>dyn Trait</code> parameters mostly apply to <code>fn foo&lt;T: ?Sized&gt;(x: T) { ... }</code>as well</p>



<a name="245752751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245752751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245752751">(Jul 12 2021 at 21:29)</a>:</h4>
<p>Indeed, <code>dyn Trait</code> is a special case of <code>T:?Sized</code>.</p>



<a name="245756938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245756938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245756938">(Jul 12 2021 at 22:11)</a>:</h4>
<p>FWIW on LLVM this (unsized params) should also work without <code>alloca</code>... indeed I hope it does since what our backend does with <code>alloca</code> in LLVM is wrong^^ (it doesnt properly account for alignment)</p>



<a name="245757064"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245757064" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245757064">(Jul 12 2021 at 22:12)</a>:</h4>
<p>so if we decide to "de-support" more general unsized locals we could remove that incorrect <code>alloca</code> code and I could sleep better knowing for sure it wouldn't be used. ;)  I think we could also remove some ugly hacks from Miri/CTFE.</p>



<a name="245762334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245762334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245762334">(Jul 12 2021 at 23:20)</a>:</h4>
<p>We'd want a magic intrinsic that allows calling a function with a unsized argument that is a dereferenced pointer, since <code>ptr::read</code> won't work due to needing to return the unsized value, or maybe we could make ptr::read magic?</p>



<a name="245762600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245762600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jacob Lifshay <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245762600">(Jul 12 2021 at 23:24)</a>:</h4>
<p>or just write</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">my_fn</span><span class="p">(</span><span class="n">_</span>: <span class="nc">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">todo!</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="n">move_call</span><span class="p">(</span><span class="n">f</span>: <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="k">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">my_fn</span><span class="p">(</span><span class="k">move</span><span class="w"> </span><span class="o">*</span><span class="n">f</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="245848778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/245848778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#245848778">(Jul 13 2021 at 16:15)</a>:</h4>
<p>I feel like maintaining a "no-alloca" rule makes sense</p>



<a name="246028390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/246028390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#246028390">(Jul 14 2021 at 22:09)</a>:</h4>
<p>FWIW, and summarizing some things mentioned in this thread, <code>unsized_fn_params</code> is basically sugar for <em>one</em> case of <code>&amp;move</code>, right?</p>
<p>That is, <code>fn foo(x: dyn Trait)</code> and <code>fn foo(x: &amp;move dyn Trait)</code> would mean the same, should both ever coexist?</p>
<p>Let's then note that the latter is more general, and less magic / more explicit <em>w.r.t.</em> semantics involved, so I do hope the one case of <code>unsized_fn_params</code> does not shadow completely the <code>&amp;move</code> ideas.</p>
<hr>
<p>One example is <em>returning</em> unsized values. AFAIUI, <code>unsized_fn_params</code> would not allow this, even though it could allow it when the returned value comes from a parameter.</p>
<p>Similarly, one can imagine:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">type</span> <span class="nc">Slot</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">MaybeUninit</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">;</span><span class="w"> </span><span class="c1">// feel free to newtype</span>
<span class="k">fn</span> <span class="nf">alloc_in</span><span class="p">(</span><span class="n">storage</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="nb">_</span> <span class="nc">mut</span><span class="w"> </span><span class="n">Slot</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">,</span><span class="w"> </span><span class="n">value</span>: <span class="nc">T</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="o">'</span><span class="nb">_</span> <span class="nc">move</span><span class="w"> </span><span class="n">T</span><span class="w"></span>
</code></pre></div>
<p>and thus, for returning an unsized type, assuming existential <code>impl</code> trait in parameter position, one could imagine:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">mk_buf</span><span class="p">(</span><span class="n">storage</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="nb">_</span> <span class="nc">mut</span><span class="w"> </span><span class="n">Slot</span><span class="o">&lt;</span><span class="cp">#[existential]</span><span class="w"> </span><span class="k">impl</span><span class="w"> </span><span class="nb">Sized</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="o">'</span><span class="nb">_</span> <span class="nc">move</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">alloc_in</span><span class="p">(</span><span class="n">storage</span><span class="p">,</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="mi">42</span><span class="p">])</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<ul>
<li>(Existential in param position should be quite trivial to feature, by using TAIT and allowing the action of <em>setting</em> assigning to a value of that type (or calling a function that does so) to be a defining use.)</li>
</ul>
<p>This further allows the caller to have control as to whether they want to stack-allocate the return value of <code>buf</code> within their own stack frame:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">caller</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">buf</span>: <span class="kp">&amp;</span><span class="nc">move</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mk_buf</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">slot</span><span class="p">());</span><span class="w"></span>
<span class="w">    </span><span class="cm">/* buf can be used here… */</span><span class="w"></span>
<span class="w">    </span><span class="cm">/* but it can't outlive this frame since its backing storage (`slot()`) gets deallocated */</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>or transitively require caller-provided storage:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">mk_buf_sorted</span><span class="p">(</span><span class="n">storage</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="nb">_</span> <span class="nc">mut</span><span class="w"> </span><span class="n">Slot</span><span class="o">&lt;</span><span class="cp">#[existential]</span><span class="w"> </span><span class="k">impl</span><span class="w"> </span><span class="nb">Sized</span><span class="o">&gt;</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="o">'</span><span class="nb">_</span> <span class="nc">move</span><span class="w"> </span><span class="p">[</span><span class="kt">u8</span><span class="p">]</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">buf</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">mk_buf</span><span class="p">(</span><span class="n">storage</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">buf</span><span class="p">.</span><span class="n">sort_unstable</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="n">buf</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I hope I am indeed proving how the value of <code>&amp;move</code> is greater than that of <code>unsized_fn_params</code>; so, if featuring <em>both</em> were to be deemed "too much", as has been said in the past in some IRLO threads about move references, I do hope Rust does not puts itself into a corner by going for the fancy-looking "<code>dyn</code> by value" over the more explicit (and thus clearer) as well as more capable <code>&amp;move</code> references.</p>
<hr>
<p>If, on the other hand, <code>unsized_fn_params</code> is not to preclude <code>&amp;move</code> references, then do clearly mention that in the RFC, and then feel free to disregard this post <span aria-label="big smile" class="emoji emoji-1f604" role="img" title="big smile">:big_smile:</span></p>



<a name="246028626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/246028626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#246028626">(Jul 14 2021 at 22:11)</a>:</h4>
<p><span class="user-mention silent" data-user-id="229517">Jacob Lifshay</span> <a href="#narrow/stream/213817-t-lang/topic/Passing.20dyn.20by.20value/near/245762600">said</a>:</p>
<blockquote>
<p>or just write</p>
<p><div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">my_fn</span><span class="p">(</span><span class="n">_</span>: <span class="nc">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">todo!</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="n">move_call</span><span class="p">(</span><span class="n">f</span>: <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="k">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">my_fn</span><span class="p">(</span><span class="k">move</span><span class="w"> </span><span class="o">*</span><span class="n">f</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div><br>
</p>
</blockquote>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">my_fn</span><span class="p">(</span><span class="n">_</span>: <span class="kp">&amp;</span><span class="nc">move</span><span class="w"> </span><span class="k">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="fm">todo!</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="n">move_call</span><span class="p">(</span><span class="n">f</span>: <span class="o">*</span><span class="k">mut</span><span class="w"> </span><span class="k">dyn</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">my_fn</span><span class="p">(</span><span class="o">&amp;</span><span class="k">move</span><span class="w"> </span><span class="o">*</span><span class="n">f</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="246029751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/246029751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#246029751">(Jul 14 2021 at 22:23)</a>:</h4>
<p><span class="user-mention" data-user-id="271719">@Mario Carneiro</span> since you seemed to have a bunch of questions regarding the exact <em>semantics</em> of these things, I can provide you a fully working prototype of <code>&amp;move</code> references as a third-party lib which <em>may</em> clarify the rules of <code>unsized_fn_params</code>, should these behave as sugared <code>&amp;move</code>s: <a href="https://docs.rs/stackbox">https://docs.rs/stackbox</a></p>



<a name="246029920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/246029920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bstrie <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#246029920">(Jul 14 2021 at 22:25)</a>:</h4>
<p>I'd be happy if the feature could be defined and implemented in terms of <code>&amp;move</code>, in the same way that async is on top of generators</p>



<a name="246057071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/246057071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#246057071">(Jul 15 2021 at 06:40)</a>:</h4>
<p>The <code>T</code> -&gt; <code>&amp;move T</code> "lowering" already happens for sized types that don't fit in two registers. This is unavoidable. <code>unsized_fn_params</code> is just a natural extension to this. I also don't think this should be blocked on <code>&amp;move</code>.</p>



<a name="246164217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/Passing%20dyn%20by%20value/near/246164217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/Passing.20dyn.20by.20value.html#246164217">(Jul 15 2021 at 22:28)</a>:</h4>
<p>True, although that only happens for <code>extern "Rust"</code>: when writing an <code>extern "C"</code> function, for instance, one needs to be very mindful of by-value _vs._ by-ref designs when optimizing stack usage and trying to elide copies, which these implicit unsugarings do not allow.</p>
<p>Note that I'm not advocating for blocking either feature, here <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span> I believe by-value <code>dyn</code> parameters will be a very welcome addition to the language; and indeed one potentially faster to feature than <code>&amp;move</code> since it does not require an addition to the language's grammar (as I see it, there is no other issue besides grammar for supporting <code>&amp;move</code>: <code>StackBox&lt;'frame, T&gt;</code> semantics are pretty clear). I am just asking for a very explicit mention in the RFC/MCP acknowledging that by-value <code>dyn</code> parameters does not cover all the spectrum of features that <code>&amp;move</code> does, and thus that neither feature is to shadow or supersede the other <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>